import socket
import sys

class RobotConnection:
    def __init__(self, ip):
        # Create a socket that sends to ip on port 3141
        self.send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.send.connect((ip, 3141))
        # Create a socket that listens to packets from ip on local port 3141
        self.receive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.receive.bind((ip, 3141))

        # Create a socket for field control that sends to ip on port 3140
        self.fcSend = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.fcSend.connect((ip, 3140))

        self.packet = [
            # Initialize analog bytes (8)
            127, 127, 127, 127, 127, 127, 127, 127,
            # Initialize digital bytes (8)
            0, 0, 0, 0, 0, 0, 0, 0
            ]

    def send_packet(self):
        string = ''
        for byte in self.data: string += chr(byte)
        self.send.send(string)

    def set_analog(self, ch, val):
        """
        Set the value of an analog channel
        ch in range [0,7]
        val in range [0, 255]
        """
        self.packet[ch] = val
	
	def set_digial(self, ch, val):
		"""
		Set the value of a digial channel
		ch in range [0,63]
		val	in one of [0,1]
		"""
		self.packet[ch//8 + 8] = self.packet[ch//8 + 8] | val << (ch%8)


def main():
    if len(sys.argv) != 2:
        print "Usage: %s TEAM#" % (sys.argv[0])
        return

    ip = '192.168.1.' + str(int(sys.argv[1]))
    robot = RobotConnection(ip)


if __name__ == '__main__':
    main()
